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