OSDN Git Service

implement MlpInfo class in Utils
authorcocot <cocot@users.sourceforge.jp>
Tue, 3 Mar 2009 14:57:40 +0000 (23:57 +0900)
committercocot <cocot@users.sourceforge.jp>
Tue, 3 Mar 2009 14:57:40 +0000 (23:57 +0900)
src/Utils.cc
src/Utils.h

index 836f543..3503d8e 100644 (file)
@@ -150,22 +150,23 @@ 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 std::vector<byte> vctr)
+{
+    pByte array = pByte(new byte[vctr.size()]);
+    for(int i = 0; i < vctr.size(); i++)
+    {
+        array[i] = vctr.at(i);
+    } 
+    return array;
+}
 
+const int DATA_HEADER = 5;
 StreamInfo::StreamInfo(pByte data, int index)throw(std::invalid_argument)
 {
     if(!data)
         throw std::invalid_argument("stream data is NULL");
 
-    if(sizeof(data.get()) + index < 5)
+    if(sizeof(data.get()) + index < DATA_HEADER)
         throw std::invalid_argument("stream data too short");
 
     uint descLength = (data[3 + index] & 0x0f) << 8 + data[4 + index];
@@ -178,7 +179,7 @@ StreamInfo::StreamInfo(pByte data, int index)throw(std::invalid_argument)
         throw std::invalid_argument("stream data too short");
     }
 
-    mData = pByte(new byte[5 + descLength]);
+    mData = pByte(new byte[DATA_HEADER + descLength]);
     for(int i = 0; i < sizeof(mData.get()); i++)
     {
         mData[i] = data[i + index];
@@ -243,7 +244,7 @@ void StreamInfo::SetSamplingFrequency(SamplingFrequency sampling_frequency)
 
 StreamInfo::StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid)
 {
-    mData = pByte(new byte[5]);
+    mData = pByte(new byte[DATA_HEADER]);
     SetElementaryStreamTypes(streamType);
     SetElementaryPID(elementaryPid);
     // reserved and descriptors length
@@ -279,16 +280,16 @@ void StreamInfo::SetElementaryPID(ushort pid)
 pByte StreamInfo::GetElementaryDescriptors(void)
 {
     pByte descriptors;
-    if(sizeof(mData.get()) == 5)
+    if(sizeof(mData.get()) == DATA_HEADER)
     {
         descriptors.reset();
     }
     else
     {
-        descriptors = pByte(new byte[sizeof(mData.get()) - 5]);
-        for (int i = 0; i < (sizeof(mData.get()) - 5); i++)
+        descriptors = pByte(new byte[sizeof(mData.get()) - DATA_HEADER]);
+        for (int i = 0; i < (sizeof(mData.get()) - DATA_HEADER); i++)
         {
-            descriptors[i] = mData[i + 5];
+            descriptors[i] = mData[i + DATA_HEADER];
         }
     }
     return descriptors;
@@ -299,10 +300,10 @@ void StreamInfo::SetElementaryDescriptors(pByte value)
 {
     if(!value || 0 == sizeof(value.get()))
     {
-        if(sizeof(mData.get()) > 5)
+        if(sizeof(mData.get()) > DATA_HEADER)
         {
             // need to remove existing descriptors
-            pByte data = pByte(new byte[5]);
+            pByte data = pByte(new byte[DATA_HEADER]);
             data[0] = mData[0];
             data[1] = mData[1];
             data[2] = mData[2];
@@ -322,7 +323,7 @@ void StreamInfo::SetElementaryDescriptors(pByte value)
             throw std::invalid_argument("descriptors data too long");
         }
 
-        pByte data = pByte(new byte[5 + sizeof(value.get())]);
+        pByte data = pByte(new byte[DATA_HEADER + sizeof(value.get())]);
         data[0] = mData[0];
         data[1] = mData[1];
         data[2] = mData[2];
@@ -330,7 +331,7 @@ void StreamInfo::SetElementaryDescriptors(pByte value)
         data[4] = (byte)(sizeof(value.get()) & 0xff);
         for (int i = 0; i < sizeof(value.get()); i++)
         {
-            data[5 + i] = value[i];
+            data[DATA_HEADER + i] = value[i];
         }
         mData = data;
     }
@@ -441,7 +442,7 @@ byte TsPacket::GetPointerSize(void)
 {
     if ((mData[3] & 0x20) == 0) // No adaptation field present
         return mData[4];
-    return (byte)(mData[4] + 1 + mData[mData[4] + 5]);
+    return (byte)(mData[4] + 1 + mData[mData[4] + DATA_HEADER]);
 }
 
 pByte TsPacket::GetData(void)
@@ -465,7 +466,7 @@ bool TsPacket::HasPcr(void)
 {
     if ((mData[3] & 0x20) > 0  // Adaptation field present
      && (mData[4] > 0)         // length > 0
-     && (mData[5] & 0x10) > 0) // and a PCR
+     && (mData[DATA_HEADER] & 0x10) > 0) // and a PCR
         return true;
     return false;
 }
@@ -605,12 +606,12 @@ bool TsTable::Complete(void)
 
 byte TsTable::GetTableId(void)
 {
-    return mData[5 + GetPointerSize()];
+    return mData[DATA_HEADER + GetPointerSize()];
 }
 
 void TsTable::SetTableId(byte value)
 {
-    mData[5 + GetPointerSize()] = value;
+    mData[DATA_HEADER + GetPointerSize()] = value;
 }
 
 ushort TsTable::GetNumberId(void)
@@ -642,10 +643,10 @@ void TsTable::SetLength(ushort value)
 void TsTable::RefreshCrc(void)
 {
     uint crc = Constants::ComputeCrc(
-        mData, GetLength() - 1, 5 + GetPointerSize());
+        mData, GetLength() - 1, DATA_HEADER + GetPointerSize());
     mData[GetLength() + 4 + GetPointerSize()]
         = (byte)((crc >> 24) & 0xff);
-    mData[GetLength() + 5 + GetPointerSize()]
+    mData[GetLength() + DATA_HEADER + GetPointerSize()]
         = (byte)((crc >> 16) & 0xff);
     mData[GetLength() + 6 + GetPointerSize()]
         = (byte)((crc >> 8) & 0xff);
@@ -693,7 +694,7 @@ PcrPacket::PcrPacket(Int64 pcr, byte counter, ushort pid)
     mData[3] &= 0x0f; // adaptation field only, no payload
     mData[3] |= 0x20; // adaptation field only, no payload
     mData[4] = 183; // length
-    mData[5] = 0x10; // only PCR present
+    mData[DATA_HEADER] = 0x10; // only PCR present
     Int64 tsClockValue = pcr / 300;
     Int64 tsOffsetValue = pcr % 300;
     mData[6] = (byte)((tsClockValue & 0x1fe000000LL) >> 25);
@@ -767,7 +768,7 @@ void PesPacket::SetPID(ushort id)
 bool PesPacket::GetComplete(void)
 {
     if(sizeof(mData.get()) < 6) return false;
-    ushort len = (ushort)((mData[4] << 8) + mData[5]);
+    ushort len = (ushort)((mData[4] << 8) + mData[DATA_HEADER]);
     if(len == 0) return false;
     if(sizeof(mData.get()) != len + 6) return false;
     return true;
@@ -781,7 +782,7 @@ void PesPacket::SetComplete(bool value)
         if (sizeof(mData.get()) > (0xffff - 6))
             len = 0;
         mData[4] = (byte)((len >> 8) & 0xff);
-        mData[5] = (byte)(len & 0xff);
+        mData[DATA_HEADER] = (byte)(len & 0xff);
     }
 }
 
@@ -859,7 +860,7 @@ PesHeader::PesHeader(pByte data)throw(std::invalid_argument)
     if (data[0] != 0x00 || data[1] != 0x00 || data[2] != 0x01)
         throw std::invalid_argument("Invalid PES prefix");
     int hlen = 9 + data[8];
-    int plen = 6 + (data[4] << 8) + data[5];
+    int plen = 6 + (data[4] << 8) + data[DATA_HEADER];
     if (plen != 6 && hlen > plen)
         throw new std::invalid_argument("Invalid PES header/packet length");
     if (sizeof(data.get()) < hlen)
@@ -896,7 +897,7 @@ int PesHeader::GetTotalHeaderLength(void)
 
 ushort PesHeader::GetPacketLength(void)
 {
-    return (ushort)((mData[4] << 8) + mData[5]);
+    return (ushort)((mData[4] << 8) + mData[DATA_HEADER]);
 }
 
 bool PesHeader::HasPts(void)
@@ -1070,15 +1071,15 @@ bool VC1SequenceInfo::Valid(void)
 
 bool VC1SequenceInfo::Interlaced(void)
 {
-    if(!mData && sizeof(mData.get()) > 5)
-        return ((mData[5] & 0x40) > 0);
+    if(!mData && sizeof(mData.get()) > DATA_HEADER)
+        return ((mData[DATA_HEADER] & 0x40) > 0);
     return false;
 }
 
 bool VC1SequenceInfo::DisplayExt(void)
 {
-    if(!mData && sizeof(mData.get()) > 5)
-        return ((mData[5] & 0x02) > 0);
+    if(!mData && sizeof(mData.get()) > DATA_HEADER)
+        return ((mData[DATA_HEADER] & 0x02) > 0);
     return false;
 }
 
@@ -1326,7 +1327,100 @@ bool DTCP_Descriptor::GetMacrovision(void)
     return ((mData[5] & 0x3) > 0);
 }
 
+MlpInfo::MlpInfo(pByte data, int offset)
+{
+    UInt32 marker = 0xffffffff;
+    for (; offset < sizeof(data.get()); offset++)
+    {
+        marker = (UInt32)marker << 8;
+        marker &= 0xffffff00;
+        marker += data[offset];
+        if (marker == Constants::MLP_SYNC)
+            break;
+    }
+    offset++;
+    if (offset < sizeof(data.get()))
+    {
+        // sequence header
+        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();
+}
+
+AspectRatio MlpInfo::GetAspectRatio(void)
+{
+    return AR_Reserved;
+}
 
+FrameRate MlpInfo::GetFrameRate(void)
+{
+    return FR_Reserved;
+}
+
+VideoFormat MlpInfo::GetVideoFormat(void)
+{
+    return VF_Reserved;
+}
+
+pByte MlpInfo::GetElementaryDescriptors(void)
+{
+    std::vector<byte> descriptors;
+    for(int i = 0; i < sizeof(Constants::ac3_registration_descriptor); i++)
+    {
+        descriptors.push_back(Constants::ac3_registration_descriptor[i]);
+    }
+    pByte ad = GetAC3AudioDescriptor();
+    for(int i = 0; i < sizeof(ad.get()); i++)
+    {
+       descriptors.push_back(ad[i]); 
+    }
+    return Utility::ToArray(descriptors);
+}
+
+AudioPresentationType MlpInfo::GetAudioPresentationType(void)
+{
+    return multi;
+}
+
+SamplingFrequency MlpInfo::GetSamplingFrequency(void)
+{
+    switch (GetSampleRateCode())
+    {
+    case 0:
+        return kHz48;
+    case 1:
+        return kHz96;
+    case 2:
+        return kHz192;
+    case 8: // 44.1kHz
+    case 9: // 88.2kHz
+    case 10: // 176.4kHz
+    default:
+        return SF_Reserved;
+    }
+}
+
+pByte MlpInfo::GetAC3AudioDescriptor(void)
+{
+    std::vector<byte> desc;
+    desc.push_back(0x81);
+    desc.push_back(0x00);
+    desc.push_back((byte)((GetSampleRateCode() << 5) | 0x08));
+    desc.push_back(200);
+    desc.push_back((byte)(0x0f));
+    desc[1] = (byte)(desc.size() - 2);
+    return Utility::ToArray(desc);
+}
+
+byte MlpInfo::GetSampleRateCode(void)
+{
+    if (sizeof(mData.get()) > 0)
+        return (byte)(mData[0] >> 4);
+    return 0x00;
+}
 
 // implement class BluRayOutput
 // implement class SitPacket : TsTable
@@ -1334,7 +1428,6 @@ bool DTCP_Descriptor::GetMacrovision(void)
 // implement class H264Info : ElementaryParse
 // implement class AC3Info : ElementaryParse
 // implement class DtsInfo : ElementaryParse
-// implement class MlpInfo : ElementaryParse
 // implement class Mpeg2Info : ElementaryParse
 
 
index 87ee521..1f411c4 100644 (file)
@@ -102,13 +102,14 @@ struct EpElement {
   EpElement(signed long long pts, unsigned long spn);
 };
 
-//class Utility {
-// public:
-//  static pByte ToArray(pByte vector);
-//};
+class Utility {
+ public:
+  static pByte ToArray(std::vector<byte> vector);
+};
 
 class StreamInfo {
  public:
+  static const int DATA_HEADER;
   StreamInfo(pByte data, int index)throw(std::invalid_argument);
   StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid);
   VideoFormat GetVideoFormat(void);
@@ -544,20 +545,14 @@ class DtsInfo : ElementaryParse {
 class MlpInfo : ElementaryParse {
  public:
   MlpInfo(pByte data, int offset);
-  VideoFormat GetVideoFormati(void);
-  void SetVideoFormat(VideoFormat videoformat);
+  VideoFormat GetVideoFormat(void);
   AspectRatio GetAspectRatio(void);
-  void SetAspectRatio(AspectRatio aspectratio);
   FrameRate GetFrameRate(void);
-  void SetFrameRate(FrameRate frameRate);
   AudioPresentationType GetAudioPresentationType(void);
-  void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
-  SamplingFrequency GetsamplingFrequency(void);
-  void SetSamplingFrequency(SamplingFrequency samplingFrequency);
+  SamplingFrequency GetSamplingFrequency(void);
   pByte GetElementaryDescriptors(void);
- private:
-  pByte AC3AudioDescriptor;
-  byte SampleRateCode;
+  pByte GetAC3AudioDescriptor(void);
+  byte GetSampleRateCode(void);
 };
 
 class Mpeg2Info : ElementaryParse {
@@ -571,7 +566,7 @@ class Mpeg2Info : ElementaryParse {
   void SetFrameRate(FrameRate frameRate);
   AudioPresentationType GetAudioPresentationType(void);
   void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
-  SamplingFrequency GetsamplingFrequency(void);
+  SamplingFrequency GetSamplingFrequency(void);
   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
   pByte GetElementaryDescriptors(void);
  private: