MythTV  master
pespacket.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Copyright (c) 2003-2004, Daniel Thor Kristjansson
3 #ifndef PES_PACKET_H
4 #define PES_PACKET_H
5 
6 /*
7  max length of PSI table = 1024 bytes
8  max length of private_section = 4096 bytes
9 */
10 
11 #include <vector>
12 using namespace std;
13 
14 using AspectArray = std::array<float,16>;
15 
16 #include "tspacket.h"
17 #include "mythlogging.h"
18 
19 MTV_PUBLIC unsigned char *pes_alloc(uint size);
20 MTV_PUBLIC void pes_free(unsigned char *ptr);
21 
28 {
29  protected:
31  PESPacket() = default;
32 
33  public:
34  // does not create it's own data
35  explicit PESPacket(const unsigned char *pesdata)
36  : m_pesData(const_cast<unsigned char*>(pesdata)),
37  m_fullBuffer(const_cast<unsigned char*>(pesdata))
38  {
39  m_badPacket = !VerifyCRC();
40  m_pesDataSize = max(((int)Length())-1 + (PESPacket::HasCRC() ? 4 : 0), 0);
41  }
42 
43  // Deleted functions should be public.
44  //const PESPacket& operator=(const PESPacket& pkt);
45  PESPacket& operator=(const PESPacket& pkt) = delete;
46 
47  // may be modified
48  PESPacket(const PESPacket& pkt)
49  : m_psiOffset(pkt.m_psiOffset),
50  m_ccLast(pkt.m_ccLast),
51  m_pesDataSize(pkt.m_pesDataSize),
52  m_allocSize(pkt.m_allocSize),
53  m_badPacket(pkt.m_badPacket)
54  { // clone
55  if (!m_allocSize)
56  m_allocSize = pkt.m_pesDataSize + (pkt.m_pesData - pkt.m_fullBuffer);
57 
58  m_fullBuffer = pes_alloc(m_allocSize);
59  memcpy(m_fullBuffer, pkt.m_fullBuffer, m_allocSize);
60  m_pesData = m_fullBuffer + (pkt.m_pesData - pkt.m_fullBuffer);
61  }
62 
63  // At this point we should have the entire VCT table in buffer
64  // at (buffer - 8), and without the tspacket 4 byte header
65 
66  //if (TableID::TVCT == table_id)
67  //VirtualChannelTable vct;
68 
69  virtual ~PESPacket()
70  {
71  if (IsClone())
72  pes_free(m_fullBuffer);
73 
74  m_fullBuffer = nullptr;
75  m_pesData = nullptr;
76  }
77 
78  bool IsClone() const { return bool(m_allocSize); }
79 
80  // return true if complete or broken
81  bool AddTSPacket(const TSPacket* tspacket, bool &broken);
82 
83  bool IsGood() const { return !m_badPacket; }
84 
85  const TSHeader* tsheader() const
86  { return reinterpret_cast<const TSHeader*>(m_fullBuffer); }
88  { return reinterpret_cast<TSHeader*>(m_fullBuffer); }
89 
90  void GetAsTSPackets(vector<TSPacket> &output, uint cc) const;
91 
92  // m_pesData[-3] == 0, m_pesData[-2] == 0, m_pesData[-1] == 1
93  uint StreamID() const { return m_pesData[0]; }
94  uint Length() const
95  { return (m_pesData[1] & 0x0f) << 8 | m_pesData[2]; }
96  // 2 bits "10"
97  // 2 bits "PES_scrambling_control (0 not scrambled)
99  { return (m_pesData[3] & 0x30) >> 4; }
101  bool HighPriority() const { return ((m_pesData[3] & 0x8) >> 3) != 0; }
103  bool DataAligned() const { return ((m_pesData[3] & 0x4) >> 2) != 0; }
108  bool CopyRight() const { return ((m_pesData[3] & 0x2) >> 1) != 0; }
110  bool OriginalRecording() const { return (m_pesData[3] & 0x1) != 0; }
111 
113  bool HasPTS() const { return ((m_pesData[4] & 0x80) >> 7) != 0; }
115  bool HasDTS() const { return ((m_pesData[4] & 0x40) >> 6) != 0; }
117  bool HasESCR() const { return ((m_pesData[4] & 0x20) >> 5) != 0; }
119  bool HasESR() const { return ((m_pesData[4] & 0x10) >> 4) != 0; }
121  bool HasDSM() const { return ((m_pesData[4] & 0x8) >> 3) != 0; }
123  bool HasACI() const { return ((m_pesData[4] & 0x4) >> 2) != 0; }
125  virtual bool HasCRC() const { return ((m_pesData[4] & 0x2) >> 1) != 0; }
127  bool HasExtensionFlags() const { return (m_pesData[4] & 0x1) != 0; }
128 
130  uint64_t PTS(void) const
131  {
132  int i = 6;
133  return
134  (uint64_t(m_pesData[i+0] & 0x0e) << 29) |
135  (uint64_t(m_pesData[i+1] ) << 22) |
136  (uint64_t(m_pesData[i+2] & 0xfe) << 14) |
137  (uint64_t(m_pesData[i+3] ) << 7) |
138  (uint64_t(m_pesData[i+4] & 0xfe) >> 1);
139  }
141  uint64_t DTS(void) const
142  {
143  int i = 6+(HasPTS()?5:0);
144  return
145  (uint64_t(m_pesData[i+0] & 0x0e) << 29) |
146  (uint64_t(m_pesData[i+1] ) << 22) |
147  (uint64_t(m_pesData[i+2] & 0xfe) << 14) |
148  (uint64_t(m_pesData[i+3] ) << 7) |
149  (uint64_t(m_pesData[i+4] & 0xfe) >> 1);
150  }
151 
152  // 8 bits PES Header Length
153  // variable length -- pes header fields
154  // variable length -- pes data block
155 
156  uint TSSizeInBuffer() const { return m_pesDataSize; }
157  uint PSIOffset() const { return m_psiOffset; }
158 
159  const unsigned char* pesdata() const { return m_pesData; }
160  unsigned char* pesdata() { return m_pesData; }
161 
162  const unsigned char* data() const { return m_fullBuffer; }
163  unsigned char* data() { return m_fullBuffer; }
164 
165  void SetStreamID(uint id) { m_pesData[0] = id; }
166  void SetLength(uint len)
167  {
168  m_pesData[1] = (m_pesData[1] & 0xf0) | ((len>>8) & 0x0f);
169  m_pesData[2] = len & 0xff;
170  }
172  {
173  len += 4 /* for CRC */;
174  len -= 3 /* for data before data last byte of length */;
175  SetLength(len);
176  }
177 
178  void SetPSIOffset(uint offset)
179  {
180  m_psiOffset = offset;
181  m_pesData = m_fullBuffer + m_psiOffset + 1;
182  }
183 
184  uint CRC(void) const
185  {
186  if (!HasCRC() || (Length() < 1))
187  return kTheMagicNoCRCCRC;
188  uint offset = Length() - 1;
189  return ((m_pesData[offset+0]<<24) |
190  (m_pesData[offset+1]<<16) |
191  (m_pesData[offset+2]<<8) |
192  (m_pesData[offset+3]));
193  }
194 
195  void SetCRC(uint crc)
196  {
197  if (Length() < 1)
198  return;
199 
200  uint offset = Length() - 1;
201  m_pesData[offset+0] = (crc & 0xff000000) >> 24;
202  m_pesData[offset+1] = (crc & 0x00ff0000) >> 16;
203  m_pesData[offset+2] = (crc & 0x0000ff00) >> 8;
204  m_pesData[offset+3] = (crc & 0x000000ff);
205  }
206 
207  uint CalcCRC(void) const;
208  bool VerifyCRC(void) const;
209 
210  protected:
211  void Finalize() { SetCRC(CalcCRC()); }
212 
213  unsigned char *m_pesData { nullptr };
214  unsigned char *m_fullBuffer { nullptr };
215 
216  uint m_psiOffset { 0 };
217  uint m_ccLast { 255 };
218  uint m_pesDataSize { 0 };
219  uint m_allocSize { 0 };
220  bool m_badPacket { false };
221 
222  // FIXME re-read the specs and follow all negations to find out the
223  // initial value of the CRC function when its being returned
224  static const uint kTheMagicNoCRCCRC = 0xFFFFFFFF;
225 };
226 
228 {
229  public:
230  uint width(void) const { return (m_data[0] <<4) | (m_data[1]>>4); }
231  uint height(void) const { return ((m_data[1] & 0xf)<<8) | m_data[2]; }
232  uint aspectNum(void) const { return m_data[3] >> 4; }
233  uint fpsNum(void) const { return m_data[3] & 0xf; }
234  float fps(void) const { return kMpeg2Fps[fpsNum()]; }
235  float aspect(bool mpeg1) const;
236 
237  private:
238  SequenceHeader() {;} // only used via reinterpret cast
240 
241  unsigned char m_data[11] {};
242  static const AspectArray kMpeg1Aspect;
243  static const AspectArray kMpeg2Aspect;
244  static const AspectArray kMpeg2Fps;
245 };
246 
247 #endif // PES_PACKET_H
PESPacket::m_pesData
unsigned char * m_pesData
Pointer to PES data in full buffer.
Definition: pespacket.h:213
PESPacket::m_pesDataSize
uint m_pesDataSize
Number of data bytes (TS header + PES data)
Definition: pespacket.h:218
bool
bool
Definition: pxsup2dast.c:30
PESPacket::ScramblingControl
uint ScramblingControl() const
Definition: pespacket.h:98
SequenceHeader::fpsNum
uint fpsNum(void) const
Definition: pespacket.h:233
PESPacket::CopyRight
bool CopyRight() const
1 bit If true packet may contain copy righted material and is known to have once contained materiale ...
Definition: pespacket.h:108
SequenceHeader::kMpeg1Aspect
static const AspectArray kMpeg1Aspect
Definition: pespacket.h:242
PESPacket::IsClone
bool IsClone() const
Definition: pespacket.h:78
PESPacket::SetTotalLength
void SetTotalLength(uint len)
Definition: pespacket.h:171
x2
static int x2
Definition: mythsocket.cpp:61
PESPacket::pesdata
const unsigned char * pesdata() const
Definition: pespacket.h:159
PESPacket::~PESPacket
virtual ~PESPacket()
Definition: pespacket.h:69
PESPacket::tsheader
const TSHeader * tsheader() const
Definition: pespacket.h:85
cc
Definition: cc.h:10
PESPacket::SetCRC
void SetCRC(uint crc)
Definition: pespacket.h:195
SequenceHeader::fps
float fps(void) const
Definition: pespacket.h:234
PESPacket::HighPriority
bool HighPriority() const
1 bit Indicates if this is a high priority packet
Definition: pespacket.h:101
PESPacket::PSIOffset
uint PSIOffset() const
Definition: pespacket.h:157
SequenceHeader::height
uint height(void) const
Definition: pespacket.h:231
PESPacket::StreamID
uint StreamID() const
Definition: pespacket.h:93
PESPacket::tsheader
TSHeader * tsheader()
Definition: pespacket.h:87
SequenceHeader::SequenceHeader
SequenceHeader()
Definition: pespacket.h:238
PESPacket::SetPSIOffset
void SetPSIOffset(uint offset)
Definition: pespacket.h:178
PESPacket::DataAligned
bool DataAligned() const
1 bit Data alignment indicator (must be 0 for video)
Definition: pespacket.h:103
x4
static int x4
Definition: mythsocket.cpp:63
PESPacket::data
const unsigned char * data() const
Definition: pespacket.h:162
mythlogging.h
PESPacket::PESPacket
PESPacket()=default
noop constructor, only for use by derived classes
SetLength
static uint8_t * SetLength(uint8_t *Data, int Length)
Definition: dvbci.cpp:117
PESPacket::data
unsigned char * data()
Definition: pespacket.h:163
TSPacket
Used to access the data of a Transport Stream packet.
Definition: tspacket.h:167
SequenceHeader::kMpeg2Fps
static const AspectArray kMpeg2Fps
Definition: pespacket.h:244
x1
static int x1
Definition: mythsocket.cpp:60
tspacket.h
PESPacket::HasACI
bool HasACI() const
1 bit Additional Copy Info field is present
Definition: pespacket.h:123
PESPacket::Length
uint Length() const
Definition: pespacket.h:94
PESPacket::IsGood
bool IsGood() const
Definition: pespacket.h:83
PESPacket::TSSizeInBuffer
uint TSSizeInBuffer() const
Definition: pespacket.h:156
uint
unsigned int uint
Definition: compat.h:140
PESPacket::HasDTS
bool HasDTS() const
1 bit Decoding Time Stamp field is present
Definition: pespacket.h:115
PESPacket::Finalize
void Finalize()
Definition: pespacket.h:211
MTV_PUBLIC
#define MTV_PUBLIC
Definition: mythtvexp.h:15
PESPacket
Allows us to transform TS packets to PES packets, which are used to hold multimedia streams and very ...
Definition: pespacket.h:28
PESPacket::OriginalRecording
bool OriginalRecording() const
1 bit Original Recording
Definition: pespacket.h:110
AspectArray
std::array< float, 16 > AspectArray
Definition: pespacket.h:14
SequenceHeader::width
uint width(void) const
Definition: pespacket.h:230
SequenceHeader::kMpeg2Aspect
static const AspectArray kMpeg2Aspect
The negative values are screen aspect ratios, while the positive ones are pixel aspect ratios.
Definition: pespacket.h:243
PESPacket::CRC
uint CRC(void) const
Definition: pespacket.h:184
SequenceHeader::aspectNum
uint aspectNum(void) const
Definition: pespacket.h:232
PESPacket::SetStreamID
void SetStreamID(uint id)
Definition: pespacket.h:165
PESPacket::PESPacket
PESPacket(const PESPacket &pkt)
Definition: pespacket.h:48
PESPacket::pesdata
unsigned char * pesdata()
Definition: pespacket.h:160
PESPacket::SetLength
void SetLength(uint len)
Definition: pespacket.h:166
PESPacket::HasESR
bool HasESR() const
1 bit Elementary Stream Rate field is present
Definition: pespacket.h:119
TSHeader
Used to access header of a TSPacket.
Definition: tspacket.h:25
PESPacket::DTS
uint64_t DTS(void) const
Decode Time Stamp, present if HasDTS() is true.
Definition: pespacket.h:141
PESPacket::operator=
PESPacket & operator=(const PESPacket &pkt)=delete
PESPacket::HasPTS
bool HasPTS() const
1 bit Presentation Time Stamp field is present
Definition: pespacket.h:113
PESPacket::HasESCR
bool HasESCR() const
1 bit Elementary Stream Clock Reference field is present
Definition: pespacket.h:117
PESPacket::HasCRC
virtual bool HasCRC() const
1 bit Cyclic Redundancy Check present
Definition: pespacket.h:125
PESPacket::m_fullBuffer
unsigned char * m_fullBuffer
Pointer to allocated data.
Definition: pespacket.h:214
PESPacket::HasDSM
bool HasDSM() const
1 bit DSM field present (should always be false for broadcasts)
Definition: pespacket.h:121
SequenceHeader::~SequenceHeader
~SequenceHeader()
Definition: pespacket.h:239
pes_alloc
MTV_PUBLIC unsigned char * pes_alloc(uint size)
Definition: pespacket.cpp:330
SequenceHeader
Definition: pespacket.h:228
PESPacket::PTS
uint64_t PTS(void) const
Presentation Time Stamp, present if HasPTS() is true.
Definition: pespacket.h:130
output
#define output
Definition: synaesthesia.cpp:221
PESPacket::HasExtensionFlags
bool HasExtensionFlags() const
1 bit Extension flags are present
Definition: pespacket.h:127
PESPacket::PESPacket
PESPacket(const unsigned char *pesdata)
Definition: pespacket.h:35
pes_free
MTV_PUBLIC void pes_free(unsigned char *ptr)
Definition: pespacket.cpp:342