OSDN Git Service

implement PatPacket class in Utils
authorcocot <cocot@users.sourceforge.jp>
Sun, 1 Mar 2009 13:38:03 +0000 (22:38 +0900)
committercocot <cocot@users.sourceforge.jp>
Sun, 1 Mar 2009 13:38:03 +0000 (22:38 +0900)
src/Utils.cc
src/Utils.h
src/tsremuxcpp_define.h

index b33edde..7d55f91 100644 (file)
@@ -150,39 +150,38 @@ namespace TsRemux
         0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
         0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 };
 
-pByte Utility::ToArray(const Bytes vector)
-{
-    pByte array = pByte(new byte[vector.size()]);
-    for(int i = 0; i < vector.size(); i++)
-    {
-        array[i] = vector.at(i);
-    } 
-    return array;
-}
+// pByte Utility::ToArray(const Bytes vector)
+// {
+//     pByte array = pByte(new byte[vector.size()]);
+//     for(int i = 0; i < vector.size(); i++)
+//     {
+//         array[i] = vector.at(i);
+//     } 
+//     return array;
+// }
 
 StreamInfo::StreamInfo(pByte data, int index)throw(std::invalid_argument)
 {
-    if(NULL == data)
+    if(!data)
         throw std::invalid_argument("stream data is NULL");
 
-    if(std::strlen(reinterpret_cast<char*>(data.get())) + index < 5)
+    if(sizeof(data.get()) + index < 5)
         throw std::invalid_argument("stream data too short");
 
-    uint descLength = (uint)((data[3 + index] & 0x0f) << 8) + data[4 + index];
+    uint descLength = (data[3 + index] & 0x0f) << 8 + data[4 + index];
 
     if(descLength > Constants::TS_SIZE)
         throw std::invalid_argument("descriptors data too long");
 
-    if(5 + descLength
-        > std::strlen(reinterpret_cast<char*>(data.get())) - index)
+    if(5 + descLength > sizeof(data.get()) - index)
     {
         throw std::invalid_argument("stream data too short");
     }
 
-    mData.clear();
-    for(int i = 0; i < (5 + descLength); i++)
-    { 
-        mData.push_back(data[i + index]);
+    mData = pByte(new byte[5 + descLength]);
+    for(int i = 0; i < sizeof(mData.get()); i++)
+    {
+        mData[i] = data[i + index];
     }
 
     SetVideoFormat(VF_Reserved);
@@ -244,25 +243,22 @@ void StreamInfo::SetSamplingFrequency(SamplingFrequency sampling_frequency)
 
 StreamInfo::StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid)
 {
-    mData.clear();
+    mData = pByte(new byte[5]);
     SetElementaryStreamTypes(streamType);
     SetElementaryPID(elementaryPid);
     // reserved and descriptors length
-    mData.push_back(0x00);
-    mData.push_back(0x00);
-    mData.push_back(0x00);
-    mData.push_back(0xf0);
-    mData.push_back(0x00);
+    mData[3] = 0xf0;
+    mData[4] = 0x00;
 }
 
 pByte StreamInfo::GetByteData(void)
 {
-    return Utility::ToArray(mData);
+    return mData;
 }
 
 ElementaryStreamTypes StreamInfo::GetElementaryStreamTypes(void)
 {
-    return (ElementaryStreamTypes)mData.at(0);
+    return (ElementaryStreamTypes)mData[0];
 }
 
 void StreamInfo::SetElementaryStreamTypes(ElementaryStreamTypes stream_type)
@@ -271,7 +267,7 @@ void StreamInfo::SetElementaryStreamTypes(ElementaryStreamTypes stream_type)
 }
 ushort StreamInfo::GetElementaryPID(void)
 {
-    return (ushort)(((mData.at(1) & 0x1f) << 8) + mData.at(2));
+    return (ushort)(((mData[1] & 0x1f) << 8) + mData[2]);
 }
 
 void StreamInfo::SetElementaryPID(ushort pid)
@@ -283,14 +279,14 @@ void StreamInfo::SetElementaryPID(ushort pid)
 pByte StreamInfo::GetElementaryDescriptors(void)
 {
     pByte descriptors;
-    if(mData.size() == 5)
+    if(sizeof(mData.get()) == 5)
     {
         descriptors.reset();
     }
     else
     {
-        descriptors = pByte(new byte[mData.size() - 5]);
-        for (int i = 0; i < (mData.size() - 5); i++)
+        descriptors = pByte(new byte[sizeof(mData.get()) - 5]);
+        for (int i = 0; i < (sizeof(mData.get()) - 5); i++)
         {
             descriptors[i] = mData[i + 5];
         }
@@ -301,18 +297,18 @@ pByte StreamInfo::GetElementaryDescriptors(void)
 void StreamInfo::SetElementaryDescriptors(pByte value)
     throw(std::invalid_argument)
 {
-    if(NULL == value || 0 == sizeof(value))
+    if(!value || 0 == sizeof(value.get()))
     {
-        if(mData.size() > 5)
+        if(sizeof(mData.get()) > 5)
         {
             // need to remove existing descriptors
-            Bytes data;
-            data.push_back(mData.at(0));
-            data.push_back(mData.at(1));
-            data.push_back(mData.at(2));
-            data.push_back(0xf0);
-            data.push_back(0x00);
-            mData.swap(data);
+            pByte data = pByte(new byte[5]);
+            data[0] = mData[0];
+            data[1] = mData[1];
+            data[2] = mData[2];
+            data[3] = 0xf0;
+            data[4] = 0x00;
+            mData = data;
         }
         else
         {
@@ -322,28 +318,73 @@ void StreamInfo::SetElementaryDescriptors(pByte value)
     }
     else
     {
-        if(sizeof(value) > 180) {
+        if(sizeof(value.get()) > 180) {
             throw std::invalid_argument("descriptors data too long");
         }
 
-        Bytes data;
-        data.push_back(mData.at(0));
-        data.push_back(mData.at(1));
-        data.push_back(mData.at(2));
-        data.push_back(
-            (byte)(0xf0
-            | (byte)((std::strlen(reinterpret_cast<char*>(value.get())) >> 8)
-            & 0x0f)));
-        data.push_back(std::strlen(reinterpret_cast<char*>(value.get())) & 0xff);
-        for (int i = 0;
-            i < std::strlen(reinterpret_cast<char*>(value.get())); i++)
+        pByte data = pByte(new byte[5 + sizeof(value.get())]);
+        data[0] = mData[0];
+        data[1] = mData[1];
+        data[2] = mData[2];
+        data[3] = (byte)(0xf0 | (byte)((sizeof(value.get()) >> 8) & 0x0f));
+        data[4] = (byte)(sizeof(value.get()) & 0xff);
+        for (int i = 0; i < sizeof(value.get()); i++)
         {
-            data.push_back(value[i]);
+            data[5 + i] = value[i];
         }
-        mData.swap(data);
+        mData = data;
+    }
+}
+
+ProgramInfo::ProgramInfo(pByte data, int index)throw(std::invalid_argument)
+{
+    if(!data)
+        throw std::invalid_argument("program data is null");
+
+    if(sizeof(data.get()) + index < 4)
+        throw std::invalid_argument("program data too short");
+
+    mData = pByte(new byte[4]);
+    for(int i = 0; i < sizeof(mData.get()); i++)
+    {
+        mData[i] = data[i + index];
     }
 }
 
+ProgramInfo::ProgramInfo(ushort programNumber, ushort programPid)
+{
+    mData = pByte(new byte[4]);
+    SetProgramNumber(programNumber);
+    SetProgramPID(programPid);
+}
+
+ushort ProgramInfo::GetProgramNumber(void)
+{
+    return (ushort)((mData[0] << 8) + mData[1]);
+}
+
+void ProgramInfo::SetProgramNumber(ushort value)
+{
+    mData[0] = (byte)((value >> 8) & 0xff);
+    mData[1] = (byte)(value & 0xff);
+}
+
+ushort ProgramInfo::GetProgramPID(void)
+{
+    return (ushort)(((mData[2] & 0x1f) << 8) + mData[3]);
+}
+
+void ProgramInfo::SetProgramPID(ushort value)
+{
+    mData[2] = (byte)(((value >> 8) & 0x1f) | 0xe0);
+    mData[3] = (byte)(value & 0xff);
+}
+
+pByte ProgramInfo::GetData(void)
+{
+    return mData;
+}
+
 ElementaryParse::ElementaryParse(void)
 {
     indicator = 0;
@@ -358,15 +399,14 @@ byte ElementaryParse::GetNextBit(void)
 
 TsPacket::TsPacket(void)
 {
-    // initialize the packet as a NULL packet
-    mData.clear();
+    // initialize the packet as a null packet
+    mData = pByte(new byte[Constants::TS_SIZE]);
     mData[0] = Constants::SYNC_BYTE; // sync byte
     mData[1] = 0x1f; // PID = 0x1FFF
     mData[2] = 0xff; // PID == 0x1FFF
     mData[3] = 0x10; // no adaptation field
-    for (int i = 4; i < Constants::TS_SIZE; i++) {
+    for (int i = 4; i < Constants::TS_SIZE; i++)
         mData[i] = 0xff;
-    }
 }
 
 bool TsPacket::GetPriority(void)
@@ -406,15 +446,14 @@ byte TsPacket::GetPointerSize(void)
 
 pByte TsPacket::GetData(void)
 {
-    return Utility::ToArray(mData);
+    return mData;
 }
 
 void TsPacket::SetData(pByte data, int startIndex)throw(std::invalid_argument)
 {
-    if (NULL == data)
+    if (!data)
         throw std::invalid_argument("NULL packet");
-    else if (Constants::TS_SIZE
-        > std::strlen(reinterpret_cast<char*>(data.get())) - startIndex)
+    else if (Constants::TS_SIZE > sizeof(data.get()) - startIndex)
         throw std::invalid_argument("small packet");
     else if (Constants::SYNC_BYTE != data[0 + startIndex])
         throw std::invalid_argument("sync byte missing");
@@ -544,17 +583,21 @@ TsTable::TsTable(pByte data)
     SetData(data, 0);
 }
 
-void TsTable::AddData(pByte data, int offset, int len)
+void TsTable::AddData(pByte value, int offset, int len)
 {
-    for(int i = 0; i < len; i++)
-    {
-        mData.push_back(data[offset + i]);
-    }
+    pByte data = pByte(new byte[sizeof(mData.get()) + len]);
+    memcpy(reinterpret_cast<char*>(data.get()),
+        reinterpret_cast<char*>(mData.get()),
+        sizeof(mData.get()));
+    memcpy(reinterpret_cast<char*>(data.get() + sizeof(mData.get())),
+        reinterpret_cast<char*>(value.get() + offset),
+        len);
+    mData = data;
 }
 
 bool TsTable::Complete(void)
 {
-    int currentLen = mData.size() - (GetPointerSize() + 8);
+    int currentLen = sizeof(mData.get()) - (GetPointerSize() + 8);
     if (GetLength() > currentLen)
         return false;
     return true;
@@ -562,7 +605,7 @@ bool TsTable::Complete(void)
 
 byte TsTable::GetTableId(void)
 {
-    return mData.at(5 + GetPointerSize());
+    return mData[5 + GetPointerSize()];
 }
 
 void TsTable::SetTableId(byte value)
@@ -572,8 +615,8 @@ void TsTable::SetTableId(byte value)
 
 ushort TsTable::GetNumberId(void)
 {
-    return (ushort)((mData.at(8 + GetPointerSize()) << 8)
-        + mData.at(9 + GetPointerSize()));
+    return (ushort)((mData[8 + GetPointerSize()] << 8)
+        + mData[9 + GetPointerSize()]);
 }
 
 void TsTable::SetNumberId(ushort value)
@@ -584,8 +627,8 @@ void TsTable::SetNumberId(ushort value)
 
 ushort TsTable::GetLength(void)
 {
-    return (ushort)(((mData.at(6 + GetPointerSize()) & 0x0f) << 8)
-        + mData.at(7 + GetPointerSize()));
+    return (ushort)(((mData[6 + GetPointerSize()] & 0x0f) << 8)
+        + mData[7 + GetPointerSize()]);
 }
 
 void TsTable::SetLength(ushort value)
@@ -599,7 +642,7 @@ void TsTable::SetLength(ushort value)
 void TsTable::RefreshCrc(void)
 {
     uint crc = Constants::ComputeCrc(
-        Utility::ToArray(mData), GetLength() - 1, 5 + GetPointerSize());
+        mData, GetLength() - 1, 5 + GetPointerSize());
     mData[GetLength() + 4 + GetPointerSize()]
         = (byte)((crc >> 24) & 0xff);
     mData[GetLength() + 5 + GetPointerSize()]
@@ -616,29 +659,31 @@ void TsTable::RefreshCrc(void)
 
 Descriptor::Descriptor(pByte data, int startIndex)throw(std::invalid_argument)
 {
-    if (strlen(reinterpret_cast<char*>(data.get())) < 2)
+    if (sizeof(data.get()) < 2)
         throw std::invalid_argument("Invalid descriptor");
+
     if (startIndex + 2 + data[startIndex + 1]
         > strlen(reinterpret_cast<char*>(data.get())))
         throw std::invalid_argument("Invalid descriptor");
-    mData.clear();
-    for (int i = 0; i < (2 + data[startIndex + 1]); i++)
-        mData.push_back(data[i + startIndex]);
+
+    mData = pByte(new byte[2 + data[startIndex + 1]]);
+    for (int i = 0; i < sizeof(mData.get()); i++)
+        mData[i] = data[i + startIndex];
 }
 
 byte Descriptor::GetTag(void)
 {
-    return mData.at(0);
+    return mData[0];
 }
 
 byte Descriptor::GetLength(void)
 {
-    return mData.at(1);
+    return mData[1];
 }
 
 pByte Descriptor::GetData(void)
 {
-    return Utility::ToArray(mData);
+    return mData;
 }
 
 PcrPacket::PcrPacket(Int64 pcr, byte counter, ushort pid)
@@ -666,7 +711,7 @@ PcrPacket::PcrPacket(Int64 pcr, byte counter, ushort pid)
 
 PesPacket::PesPacket(pByte buff, int offset, int length, ushort pid)
 {
-    mData.assign(0,length);
+    mData = pByte(new byte[length]);
     SetPID(pid);
     AddData(buff, offset, length);
     SetPriority(false);
@@ -684,32 +729,29 @@ void PesPacket::SetPriority(bool value)
 
 pByte PesPacket::GetData(void)
 {
-    pByte buff = pByte(new byte[mData.size()+1]);
-    for(int i = 0; i < mData.size();i++)
-        buff[i] = mData.at(i);
-    buff[mData.size()] = '\0';
-    return buff;
+    return mData;
 }
 
 pByte PesPacket::GetPayload(void)
 {
     boost::shared_ptr<PesHeader> ph = GetHeader();
-    if(ph == NULL) return GetData();
-    pByte buff = pByte(new byte[mData.size() - 9 + ph.get()->GetHeaderLength() + 1]);
-    for(int i=(9 + ph.get()->GetHeaderLength()); i < mData.size(); i++)
-        buff[i] = mData.at(i);
-    buff[mData.size()-(9 + ph.get()->GetHeaderLength())] = '\0';
+    if(!ph) return GetData();
+    pByte buff = pByte(
+        new byte[sizeof(mData.get()) - 9 + ph.get()->GetHeaderLength() + 1]);
+    for(int i=(9 + ph.get()->GetHeaderLength()); i < sizeof(mData.get()); i++)
+        buff[i] = mData[i];
+    buff[sizeof(mData.get()) - (9 + ph.get()->GetHeaderLength())] = '\0';
     return buff;
 }
 
 byte PesPacket::GetByte(int i)
 {
-    return mData.at(i);
+    return mData[i];
 }
 
-void PesPacket::SetByte(byte value)
+void PesPacket::SetByte(int i, byte value)
 {
-//    mData[i] = value;
+    mData[i] = value;
 }
 
 ushort PesPacket::GetPID(void)
@@ -724,10 +766,10 @@ void PesPacket::SetPID(ushort id)
 
 bool PesPacket::GetComplete(void)
 {
-    if(mData.size() < 6) return false;
+    if(sizeof(mData.get()) < 6) return false;
     ushort len = (ushort)((mData[4] << 8) + mData[5]);
     if(len == 0) return false;
-    if(mData.size() != len + 6) return false;
+    if(sizeof(mData.get()) != len + 6) return false;
     return true;
 }
 
@@ -735,8 +777,8 @@ void PesPacket::SetComplete(bool value)
 {
     if(value)
     {
-        ushort len = (ushort)(mData.size() - 6);
-        if (mData.size() > (0xffff - 6))
+        ushort len = (ushort)(sizeof(mData.get()) - 6);
+        if (sizeof(mData.get()) > (0xffff - 6))
             len = 0;
         mData[4] = (byte)((len >> 8) & 0xff);
         mData[5] = (byte)(len & 0xff);
@@ -748,8 +790,7 @@ boost::shared_ptr<PesHeader> PesPacket::GetHeader(void)
     boost::shared_ptr<PesHeader> ph;
     try
     {
-        ph = boost::shared_ptr<PesHeader>(
-            new PesHeader(Utility::ToArray(mData)));
+        ph = boost::shared_ptr<PesHeader>(new PesHeader(mData));
         return ph;
     }
     catch(...)
@@ -762,37 +803,45 @@ boost::shared_ptr<PesHeader> PesPacket::GetHeader(void)
 
 void PesPacket::AddData(pByte moredata)
 {
-    for(std::vector<byte>::iterator it = mData.begin();
-        it != mData.end();
-        it++)
-    {
-        mData.push_back(*it);
-    }
+    pByte data = pByte(new byte[sizeof(mData.get()) + sizeof(moredata.get())]);
+    memcpy(reinterpret_cast<char*>(data.get()),
+        reinterpret_cast<char*>(mData.get()),
+        sizeof(mData.get()));
+    memcpy(reinterpret_cast<char*>(data.get() + sizeof(mData.get())),
+        reinterpret_cast<char*>(mData.get()),
+        sizeof(mData.get()));
+    mData = data;
 }
 
-void PesPacket::AddData(pByte buff, int offset, int length)
+void PesPacket::AddData(pByte buff, int offset, int len)
 {
-    for (int i = offset; i < length + offset; i++)
-        mData.push_back(buff[i]);
+    pByte data = pByte(new byte[sizeof(mData.get()) + len]);
+    memcpy(reinterpret_cast<char*>(data.get()),
+        reinterpret_cast<char*>(mData.get()),
+        sizeof(mData.get()));
+    memcpy(reinterpret_cast<char*>(data.get() + sizeof(mData.get())),
+        reinterpret_cast<char*>(mData.get() + offset),
+        len);
+    mData = data;
 }
 
 byte PesPacket::GetBaseId(void)
 {
-    if (mData.size() > 3)
+    if (sizeof(mData.get()) > 3)
         return mData[3];
     return 0;
 }
 
 byte PesPacket::GetExtendedId(void)
 {
-    if ((mData.size() > 8) && mData.size() > (8 + mData[8]))
+    if ((sizeof(mData.get()) > 8) && sizeof(mData.get()) > (8 + mData[8]))
         return mData[9 + mData[8]];
     return 0;
 }
 
 UInt32 PesPacket::GetExtendedType(void)
 {
-    if ((mData.size() > 8) && mData.size() > (11 + mData[8]))
+    if ((sizeof(mData.get()) > 8) && sizeof(mData.get()) > (11 + mData[8]))
     {
         UInt32 format = (UInt32)mData[9 + mData[8]] << 24;
         format += (UInt32)mData[10 + mData[8]] << 16;
@@ -815,9 +864,9 @@ PesHeader::PesHeader(pByte data)throw(std::invalid_argument)
         throw new std::invalid_argument("Invalid PES header/packet length");
     if (sizeof(data.get()) < hlen)
         throw std::invalid_argument("PES Header too short");
-    mData.clear();
+    mData = pByte(new byte[hlen]);
     for (int i = 0; i < hlen; i++)
-        mData.push_back(data[i]);
+        mData[i] = data[i];
 }
 
 byte PesHeader::GetStreamId(void)
@@ -827,7 +876,7 @@ byte PesHeader::GetStreamId(void)
 
 byte PesHeader::GetByte(int i)
 {
-    return mData.at(i);
+    return mData[i];
 }
 
 void PesHeader::SetByte(int i, byte data)
@@ -837,7 +886,7 @@ void PesHeader::SetByte(int i, byte data)
 
 byte PesHeader::GetHeaderLength(void)
 {
-    return mData.at(8);
+    return mData[8];
 }
 
 int PesHeader::GetTotalHeaderLength(void)
@@ -847,20 +896,20 @@ int PesHeader::GetTotalHeaderLength(void)
 
 ushort PesHeader::GetPacketLength(void)
 {
-    return (ushort)((mData.at(4) << 8) + mData.at(5));
+    return (ushort)((mData[4] << 8) + mData[5]);
 }
 
 bool PesHeader::HasPts(void)
 {
-    if(mData.size() > 13)
-        return (mData.at(7) & 0x80) > 0;
+    if(sizeof(mData.get()) > 13)
+        return (mData[7] & 0x80) > 0;
     return false;
 }
 
 bool PesHeader::HasDts(void)
 {
-    if(mData.size() > 18 )
-        return (mData.at(7) & 0x40) > 0;
+    if(sizeof(mData.get()) > 18 )
+        return (mData[7] & 0x40) > 0;
     return false;
 }
 
@@ -881,7 +930,7 @@ void PesHeader::SetPts(Int64 value)throw(std::invalid_argument)
 {
     if (HasPts() == false)
         throw std::invalid_argument("No Pts available");
-    byte old = (byte)(mData.at(9) & 0xf1);
+    byte old = (byte)(mData[9] & 0xf1);
     mData[9] = (byte)(((value & 0x1C0000000LL) >> 29) | old);
     mData[10] = (byte)((value & 0x3fC00000LL) >> 22);
     mData[11] = (byte)(((value & 0x3f8000LL) >> 14) | 0x01);
@@ -917,19 +966,19 @@ void PesHeader::SetDts(Int64 value)throw(std::invalid_argument)
 byte PesHeader::GetExtention2(void)
 {
     int offset = 6;
-    if((mData.at(offset) & 0xc0) != 0x80)
+    if((mData[offset] & 0xc0) != 0x80)
         return 0; // first two bits must be '10'
-    byte PTS_DTS_flags = (byte)(mData.at(offset + 1) & 0xc0);
-    byte ESCR_flag = (byte)(mData.at(offset + 1) & 0x20);
-    byte ES_rate_flag = (byte)(mData.at(offset + 1) & 0x10);
-    byte DSM_trick_mode_flag = (byte)(mData.at(offset + 1) & 0x08);
-    byte additional_copy_info_flag = (byte)(mData.at(offset + 1) & 0x04);
-    byte PES_CRC_flag = (byte)(mData.at(offset + 1) & 0x02);
-    byte PES_extension_flag = (byte)(mData.at(offset + 1) & 0x01);
-    if(mData.at(offset + 2) == 0)
+    byte PTS_DTS_flags = (byte)(mData[offset + 1] & 0xc0);
+    byte ESCR_flag = (byte)(mData[offset + 1] & 0x20);
+    byte ES_rate_flag = (byte)(mData[offset + 1] & 0x10);
+    byte DSM_trick_mode_flag = (byte)(mData[offset + 1] & 0x08);
+    byte additional_copy_info_flag = (byte)(mData[offset + 1] & 0x04);
+    byte PES_CRC_flag = (byte)(mData[offset + 1] & 0x02);
+    byte PES_extension_flag = (byte)(mData[offset + 1] & 0x01);
+    if(mData[offset + 2] == 0)
         return 0;
-    int length = offset + mData.at(offset + 2) + 3;
-    if(mData.size() < length)
+    int length = offset + mData[offset + 2] + 3;
+    if(sizeof(mData.get()) < length)
         return 0;
     offset += 3;
     if(PTS_DTS_flags == 0x80)
@@ -948,17 +997,17 @@ byte PesHeader::GetExtention2(void)
         offset += 2;
     if(PES_extension_flag == 0)
         return 0;
-    byte PES_private_data_flag = (byte)(mData.at(offset) & 0x80);
-    byte pack_header_field_flag = (byte)(mData.at(offset) & 0x40);
+    byte PES_private_data_flag = (byte)(mData[offset] & 0x80);
+    byte pack_header_field_flag = (byte)(mData[offset] & 0x40);
     byte program_packet_sequence_counter_flag
-        = (byte)(mData.at(offset) & 0x20);
-    byte PSTD_mDataer_flag = (byte)(mData.at(offset) & 0x10);
-    byte PES_extension_flag_2 = (byte)(mData.at(offset) & 0x01);
+        = (byte)(mData[offset] & 0x20);
+    byte PSTD_mDataer_flag = (byte)(mData[offset] & 0x10);
+    byte PES_extension_flag_2 = (byte)(mData[offset] & 0x01);
     offset++;
     if(PES_private_data_flag > 0)
         offset += 25;
     if(pack_header_field_flag > 0)
-        offset += (mData.at(offset) + 1);
+        offset += (mData[offset] + 1);
     if(program_packet_sequence_counter_flag > 0)
         offset += 2;
     if(PSTD_mDataer_flag > 0)
@@ -967,18 +1016,18 @@ byte PesHeader::GetExtention2(void)
         return 0;
     if(mData[offset] != 0x81)
         return 0;
-    return mData.at(offset + 1);
+    return mData[offset + 1];
 }
 
 pByte PesHeader::GetData(void)
 {
-    return Utility::ToArray(mData);
+    return mData;
 }
 
 VC1SequenceInfo::VC1SequenceInfo(pByte data, int offset)
 {
     UInt32 marker = 0xffffffff;
-    for(; offset < sizeof(reinterpret_cast<char*>(data.get())); offset++)
+    for(; offset < sizeof(data.get()); offset++)
     {
         marker = marker << 8;
         marker &= 0xffffff00;
@@ -987,65 +1036,63 @@ VC1SequenceInfo::VC1SequenceInfo(pByte data, int offset)
             break;
     }
     offset++;
-    mData.clear();
-    if(offset < sizeof(reinterpret_cast<char*>(data.get())))
+    if(offset < sizeof(data.get()))
     {
         // sequence header
-        for(int i = 0;
-            offset < sizeof(reinterpret_cast<char*>(data.get()));
-            i++, offset++)
-        {
-            mData.push_back(data[offset]);
-        }
+        mData = pByte(new byte[sizeof(data.get()) - offset]);
+        for(int i = 0; offset < sizeof(data.get()); i++, offset++)
+            mData[i] = data[offset];
     }
+    else
+        mData.reset();
 }
 
 int VC1SequenceInfo::GetHeight(void)
 {
-    if(!mData.empty()&& mData.size() > 4)
-        return ((((mData.at(3) & 0x0f) << 8) + mData.at(4)) << 1) + 2;
+    if(!mData && sizeof(mData.get()) > 4)
+        return ((((mData[3] & 0x0f) << 8) + mData[4]) << 1) + 2;
     else
         return -1;
 }
 
 int VC1SequenceInfo::GetWidth(void)
 {
-    if(!mData.empty() && mData.size() > 3)
-        return (((mData.at(2) << 4) + ((mData.at(3) & 0xf0) >> 4)) << 1) + 2;
+    if(!mData && sizeof(mData.get()) > 3)
+        return (((mData[2] << 4) + ((mData[3] & 0xf0) >> 4)) << 1) + 2;
     else
         return -1;
 }
 
 bool VC1SequenceInfo::Valid(void)
 {
-    return !mData.empty();
+    return !mData;
 }
 
 bool VC1SequenceInfo::Interlaced(void)
 {
-    if(!mData.empty() && mData.size() > 5)
-        return ((mData.at(5) & 0x40) > 0);
+    if(!mData && sizeof(mData.get()) > 5)
+        return ((mData[5] & 0x40) > 0);
     return false;
 }
 
 bool VC1SequenceInfo::DisplayExt(void)
 {
-    if(!mData.empty() && mData.size() > 5)
-        return ((mData.at(5) & 0x02) > 0);
+    if(!mData && sizeof(mData.get()) > 5)
+        return ((mData[5] & 0x02) > 0);
     return false;
 }
 
 bool VC1SequenceInfo::AspectFlag(void)
 {
-    if(DisplayExt() && mData.size() > 9)
-        return ((mData.at(9) & 0x10) > 0);
+    if(DisplayExt() && sizeof(mData.get()) > 9)
+        return ((mData[9] & 0x10) > 0);
     return false;
 }
 
 byte VC1SequenceInfo::GetVc1AspectRatio(void)
 {
-    if(AspectFlag() && mData.size() > 9)
-        return (byte)(mData.at(9) & 0x0f);
+    if(AspectFlag() && sizeof(mData.get()) > 9)
+        return (byte)(mData[9] & 0x0f);
     return 0;
 }
 
@@ -1053,15 +1100,15 @@ bool VC1SequenceInfo::FrameFlag(void)
 {
     if(AspectFlag())
     {
-         if(GetVc1AspectRatio() == 15 && mData.size() > 12)
-             return ((mData.at(12) & 0x80) > 0);
-         else if(GetVc1AspectRatio() != 15 && mData.size() > 10)
-             return ((mData.at(10) & 0x80) > 0);
+         if(GetVc1AspectRatio() == 15 && sizeof(mData.get()) > 12)
+             return ((mData[12] & 0x80) > 0);
+         else if(GetVc1AspectRatio() != 15 && sizeof(mData.get()) > 10)
+             return ((mData[10] & 0x80) > 0);
          else
              return false;
     }
-    else if(mData.size() > 9)
-         return ((mData.at(9) & 0x08) > 0);
+    else if(sizeof(mData.get()) > 9)
+         return ((mData[9] & 0x08) > 0);
     else
          return false;
 }
@@ -1072,15 +1119,15 @@ bool VC1SequenceInfo::FrameRateIndicatorFlag(void)
     {
         if(AspectFlag())
         {
-            if (GetVc1AspectRatio() == 15 && mData.size() > 12)
-                return ((mData.at(12) & 0x40) > 0);
-            else if (GetVc1AspectRatio() != 15 && mData.size() > 10)
-                return ((mData.at(10) & 0x40) > 0);
+            if (GetVc1AspectRatio() == 15 && sizeof(mData.get()) > 12)
+                return ((mData[12] & 0x40) > 0);
+            else if (GetVc1AspectRatio() != 15 && sizeof(mData.get()) > 10)
+                return ((mData[10] & 0x40) > 0);
             else
                 return false;
         }
-        else if (mData.size() > 9)
-            return ((mData.at(9) & 0x04) > 0);
+        else if (sizeof(mData.get()) > 9)
+            return ((mData[9] & 0x04) > 0);
         else
             return false;
     }
@@ -1139,24 +1186,24 @@ FrameRate VC1SequenceInfo::GetFrameRate(void)
         byte FrameRateDr = 0;
         if(AspectFlag())
         {
-            if(GetVc1AspectRatio() == 15 && mData.size() > 13)
+            if(GetVc1AspectRatio() == 15 && sizeof(mData.get()) > 13)
             {
-                FrameRateNr = (byte)(((mData.at(12) & 0x3f) << 2)
-                    + ((mData.at(13) & 0xc0) >> 6));
-                FrameRateDr = (byte)((mData.at(13) & 0x3c) >> 2);
+                FrameRateNr = (byte)(((mData[12] & 0x3f) << 2)
+                    + ((mData[13] & 0xc0) >> 6));
+                FrameRateDr = (byte)((mData[13] & 0x3c) >> 2);
             }
-            else if(GetVc1AspectRatio() != 15 && mData.size() > 11)
+            else if(GetVc1AspectRatio() != 15 && sizeof(mData.get()) > 11)
             {
-                FrameRateNr = (byte)(((mData.at(10) & 0x3f) << 2)
-                    + ((mData.at(11) & 0xc0) >> 6));
-                FrameRateDr = (byte)((mData.at(11) & 0x3c) >> 2);
+                FrameRateNr = (byte)(((mData[10] & 0x3f) << 2)
+                    + ((mData[11] & 0xc0) >> 6));
+                FrameRateDr = (byte)((mData[11] & 0x3c) >> 2);
             }
         }
-        else if(mData.size() > 11)
+        else if(sizeof(mData.get()) > 11)
         {
-            FrameRateNr = (byte)(((mData.at(9) & 0x03) << 6)
-                + ((mData.at(10) & 0xfc) >> 2));
-            FrameRateDr = (byte)(((mData.at(10) & 0x03) << 2)
+            FrameRateNr = (byte)(((mData[9] & 0x03) << 6)
+                + ((mData[10] & 0xfc) >> 2));
+            FrameRateDr = (byte)(((mData[10] & 0x03) << 2)
                 + ((mData[11] & 0xc0) >> 6));
         }
 
@@ -1176,12 +1223,85 @@ FrameRate VC1SequenceInfo::GetFrameRate(void)
     return FR_Reserved;
 }
 
+// implement class PatPacket : TsTable
+PatPacket::PatPacket(void)
+{
+    SetPID(Constants::PAT_PID);
+    SetTableId(Constants::PAT_TABLE_ID);
+    SetLength(9);
+    SetTransportStreamId(1);
+    RefreshCrc();
+}
+
+PatPacket::PatPacket(pByte data)throw(std::invalid_argument)
+{
+    if(GetTableId() != Constants::PAT_TABLE_ID)
+        throw std::invalid_argument(
+        "packet does not contain a valid PAT table ID");
+    if (0 != GetPID())
+        throw std::invalid_argument("packet does not contain a valid PAT PID");
+}
+
+ushort PatPacket::GetTransportStreamId(void)
+{
+    return GetNumberId();
+}
+
+void PatPacket::SetTransportStreamId(ushort value)
+{
+    SetNumberId(value);
+    RefreshCrc();
+}
+/*
+boost::shared_array<ProgramInfo> PatPacket::GetPrograms(void)
+{
+    boost::shared_array<ProgramInfo> programs;
+    programs.reset();
+    if (GetProgramInfoLength() == 0)
+        return programs;
+    programs = boost::shared_array(new ProgramInfo[GetProgramInfoLength() / 4]);
+    for (int i = 0; i < GetProgramInfoLength(); i += 4)
+        programs[i / 4] = boost::shared_array<ProgramInfo>(
+            new ProgramInfo(mData, 13 + GetPointerSize() + i));
+    return programs;
+}
+
+void PatPacket::SetPrograms(boost::shared_ptr<ProgramInfo> value)
+    throw(std::invalid_argument)
+{
+    if (NULL == value || sizeof(value.get()) == 0)
+    {
+        if (GetProgramInfoLength() == 0)
+            return;
+        SetLength(9);
+        RefreshCrc();
+    }
+    else
+    {
+        if ((sizeof(value.get()) * 4) + 17 + GetPointerSize()
+            > Constants.TS_SIZE)
+            throw std::invalid_argument("program info data too long");
+        SetLength((ushort)(9 + (sizeof(value.get()) * 4)));
+        int index = 13 + GetPointerSize();
+        for(int pi = 0; pi < GetProgramInfoLength() / 4; pi++)
+        {
+            for (int i = 0; i < 4; i++)
+                mData[index + i] = value[pi].Data[i];
+            index += 4;
+        }
+        RefreshCrc();
+    }
+}
+*/
+ushort PatPacket::GetProgramInfoLength(void)
+{
+    return (ushort)(GetLength() - 9);
+}
 
 
 
 // implement class BluRayOutput
 // implement class DTCP_Descriptor : Descriptor
-// implement class PatPacket : TsTable
 // implement class SitPacket : TsTable
 // implement class PmPacket : TsTable
 // implement class H264Info : ElementaryParse
index 6b116d6..552a75e 100644 (file)
@@ -102,10 +102,11 @@ struct EpElement {
   EpElement(signed long long pts, unsigned long spn);
 };
 
-class Utility {
- public:
-  static pByte ToArray(Bytes vector);
-};
+//class Utility {
+// public:
+//  static pByte ToArray(pByte vector);
+//};
+
 class StreamInfo {
  public:
   StreamInfo(pByte data, int index)throw(std::invalid_argument);
@@ -128,7 +129,7 @@ class StreamInfo {
   void SetElementaryDescriptors(pByte value)throw(std::invalid_argument);
   pByte GetByteData(void);
  private:
-  Bytes mData;
+  pByte mData;
   VideoFormat mVideoFormat;
   AspectRatio mAspectRatio;
   FrameRate mFrameRate;
@@ -136,7 +137,7 @@ class StreamInfo {
   SamplingFrequency mSamplingFrequency;
 //  ElementaryStreamTypes StreamType;
 //  ushort ElementaryPID;
-//  Bytes ElementaryDescriptors;
+//  pByte ElementaryDescriptors;
 };
 
 class BluRayOutput {
@@ -178,7 +179,7 @@ class Descriptor {
   byte GetTag(void);
   byte GetLength(void);
  private:
-  Bytes mData;
+  pByte mData;
 };
 
 class DTCP_Descriptor : Descriptor {
@@ -272,13 +273,15 @@ class Constants {
 
 class ProgramInfo {
  public:
-  ProgramInfo(pByte data, int index);
+  ProgramInfo(pByte data, int index)throw(std::invalid_argument);
   ProgramInfo(ushort programNumber, ushort programPid);
-  ushort ProgramNumber;
-  ushort ProgramPID;
-  Bytes Data;
+  ushort GetProgramNumber(void);
+  void SetProgramNumber(ushort programNumber);
+  ushort GetProgramPID(void);
+  void SetProgramPID(ushort programPID);
+  pByte GetData(void);
  private:
-  Bytes mData;
+  pByte mData;
 };
 
 class TsPacket {
@@ -299,7 +302,7 @@ class TsPacket {
   void SetContinuityCounter(byte value)throw(std::out_of_range);
   bool HasPesHeader(void);
  protected:
-  Bytes mData;
+  pByte mData;
  private:
   bool Priority;
   ushort PID;
@@ -310,7 +313,7 @@ class PcrPacket : TsPacket {
   PcrPacket(Int64 pcr, byte counter, ushort pid);
 };
 
-class TsTable : TsPacket {
+class TsTable : public TsPacket {
  public:
   TsTable();
   TsTable(pByte data);
@@ -326,14 +329,16 @@ class TsTable : TsPacket {
   void RefreshCrc(void);
 };
 
-class PatPacket : TsTable {
+class PatPacket : public TsTable {
  public:
   PatPacket(void);
-  PatPacket(pByte data);
-  ushort TransportStreamId;
-  ProgramInfo* Programs;
- private:
-  ushort ProgramInfoLength;
+  PatPacket(pByte data)throw(std::invalid_argument);
+  ushort GetTransportStreamId(void);
+  void SetTransportStreamId(ushort TSId);
+  boost::shared_array<ProgramInfo> GetPrograms(void);
+  void SetPrograms(boost::shared_array<ProgramInfo> programinfo)
+      throw(std::invalid_argument);
+  ushort GetProgramInfoLength(void);
 };
 
 class SitPacket : TsTable {
@@ -347,7 +352,7 @@ class PmPacket : TsTable {
   PmPacket(void);
   PmPacket(byte* data);
   DTCP_Descriptor DtcpInfo;
-  Bytes ProgramDescriptorsData;
+  pByte ProgramDescriptorsData;
   StreamInfo* ElementaryStreams;
   ushort ProgramNumber;
   ushort PcrPID;
@@ -374,7 +379,7 @@ class PesHeader {
   byte GetExtention2(void);
   pByte GetData(void);
  private:
-  Bytes mData;
+  pByte mData;
 };
 
 class PesPacket {
@@ -385,7 +390,7 @@ class PesPacket {
   pByte GetData(void);
   pByte GetPayload(void);
   byte GetByte(int i);
-  void SetByte(byte dat);
+  void SetByte(int i, byte dat);
   ushort GetPID(void);
   void SetPID(ushort id);
   bool GetComplete(void);
@@ -397,7 +402,7 @@ class PesPacket {
   byte GetExtendedId(void);
   UInt32 GetExtendedType(void);
  private:
-  Bytes mData;
+  pByte mData;
   bool mPriority;
   ushort mPID;
 };
@@ -418,7 +423,7 @@ class VC1SequenceInfo {
   VideoFormat GetVideoFormat(void);
   FrameRate GetFrameRate(void);
  private:
-  Bytes mData;
+  pByte mData;
 };
 
 enum Ac3SyntaxType {
@@ -445,7 +450,7 @@ class ElementaryParse {
   virtual pByte GetElementaryDescriptors(void);
  protected:
   byte GetNextBit(void);
-  Bytes mData;
+  pByte mData;
   int indicator;
   bool mValid;
   VideoFormat mVideoFormat;
@@ -453,13 +458,13 @@ class ElementaryParse {
   AspectRatio mAspectRatio;
   SamplingFrequency mSamplingFrequency;
   AudioPresentationType mAudioPresentationType;
-  Bytes ElementaryDescriptors;
+  pByte ElementaryDescriptors;
 };
 
 class H264Info : ElementaryParse {
  public:
   H264Info(pByte data, int offset);
-  Bytes ElementaryDescriptors;
+  pByte ElementaryDescriptors;
   VideoFormat GetVideoFormati(void);
   void SetVideoFormat(VideoFormat videoformat);
   AspectRatio GetAspectRatio(void);
@@ -475,7 +480,7 @@ class H264Info : ElementaryParse {
   void ScalingListSkip(int skip);
   UInt32 Width;
   UInt32 Heigth;
-  Bytes HdmvVideoRegistrationDescriptor;
+  pByte HdmvVideoRegistrationDescriptor;
   VideoFormat mVideoFormat;
   AspectRatio mAspectRatio;
   FrameRate   mFrameRate;
@@ -547,7 +552,7 @@ class MlpInfo : ElementaryParse {
   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
   pByte GetElementaryDescriptors(void);
  private:
-  Bytes AC3AudioDescriptor;
+  pByte AC3AudioDescriptor;
   byte SampleRateCode;
 };
 
@@ -566,7 +571,7 @@ class Mpeg2Info : ElementaryParse {
   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
   pByte GetElementaryDescriptors(void);
  private:
-  Bytes Mpeg2VideoRegistrationDescriptor;
+  pByte Mpeg2VideoRegistrationDescriptor;
   ushort Horizontal;
   ushort Vertical;
   byte Aspect;
index 17a8428..4c2dc81 100644 (file)
@@ -18,7 +18,7 @@ namespace TsRemux {
     typedef signed long long Int64;
     typedef time_t TimeSpan;
     typedef boost::shared_array<byte> pByte;
-    typedef std::vector<byte> Bytes;
-    typedef std::vector<byte>::iterator Iter;
+//    typedef std::vector<byte> Bytes;
+//    typedef std::vector<byte>::iterator Iter;
 } // namespace
 #endif TSREMUXCPP_DEFINE_H_