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