Ticket #565: offset.diff

File offset.diff, 4.0 KB (added by redmundt@…, 18 years ago)
  • pespacket.cpp

     
    99
    1010using namespace std;
    1111
    12 static inline uint calcOffset(uint cnt)
    13 {
    14     return (cnt * TSPacket::PAYLOAD_SIZE) + TSPacket::HEADER_SIZE;
    15 }
    16 
    1712// return true if complete or broken
    1813bool PESPacket::AddTSPacket(const TSPacket* packet)
    1914{
     
    3328
    3429    const int cc = packet->ContinuityCounter();
    3530    const int ccExp = (_ccLast + 1) & 0xf;
     31 
     32    uint payloadSize;
     33    uint payloadStart;
     34   
     35    // If the next TS has an offset, we need to strip it out.
     36    // The offset will be used when a new PESPacket is created.
     37    if (packet->PayloadStart())
     38    {
     39        payloadSize = TSPacket::PAYLOAD_SIZE - 1;
     40        payloadStart = TSPacket::HEADER_SIZE + 1;
     41    }
     42    else
     43    {
     44        payloadSize = TSPacket::PAYLOAD_SIZE;
     45        payloadStart = TSPacket::HEADER_SIZE;
     46    }
     47
    3648    if (ccExp == cc)
    3749    {
    38         if (calcOffset(_cnt+1) >= _allocSize)
     50        if (_pesdataSize + payloadSize >= _allocSize)
    3951        {
    4052            uint sz = (((_allocSize * 2) + 4095) / 4096) * 4096;
    4153            unsigned char *nbuf = pes_alloc(sz);
    42             memcpy(nbuf, _fullbuffer, _allocSize);
     54            memcpy(nbuf, _fullbuffer, _pesdataSize);
    4355            pes_free(_fullbuffer);
    4456            _fullbuffer = nbuf;
    4557            _allocSize  = sz;
    4658        }
    47         memcpy(_fullbuffer    + calcOffset(_cnt),
    48                packet->data() + TSPacket::HEADER_SIZE,
    49                TSPacket::PAYLOAD_SIZE);
    5059
     60        memcpy(_fullbuffer    + _pesdataSize,
     61               packet->data() + payloadStart,
     62               payloadSize);
     63
    5164        _ccLast = cc;
    52         _cnt++;
     65        _pesdataSize += payloadSize;
    5366    }
    5467    else if (int(_ccLast) == cc)
    5568    {
     
    6275        return true;
    6376    }
    6477
    65     if (calcOffset(_cnt) >= tlen)
     78    if (_pesdataSize >= tlen)
    6679    {
    6780        if (CalcCRC()==CRC())
    6881        {
  • pespacket.h

     
    4949  protected:
    5050    // does not create it's own data
    5151    PESPacket(const TSPacket* tspacket, bool)
    52         : _pesdata(0), _fullbuffer(0), _allocSize(0)
     52        : _pesdata(0), _fullbuffer(0), _allocSize(0), _pesdataSize(188)
    5353    {
    5454        InitPESPacket(const_cast<TSPacket&>(*tspacket));
    5555        _fullbuffer = const_cast<unsigned char*>(tspacket->data());
     
    6464    PESPacket(const PESPacket& pkt)
    6565        : _pesdata(0),
    6666          _psiOffset(pkt._psiOffset),
    67           _ccLast(pkt._ccLast), _cnt(pkt._cnt),
     67          _ccLast(pkt._ccLast),
    6868          _allocSize((pkt._allocSize) ? pkt._allocSize : 188),
     69          _pesdataSize(pkt._pesdataSize),
    6970          _badPacket(pkt._badPacket)
    7071    { // clone
    7172        _fullbuffer = pes_alloc(_allocSize);
     
    7576
    7677    // may be modified
    7778    PESPacket(const TSPacket& tspacket)
    78         : _ccLast(tspacket.ContinuityCounter()), _cnt(1)
     79        : _ccLast(tspacket.ContinuityCounter()), _pesdataSize(188)
    7980    { // clone
    8081        InitPESPacket(const_cast<TSPacket&>(tspacket)); // sets _psiOffset
    8182
     
    118119    unsigned int Length() const
    119120        { return (pesdata()[2] & 0x0f) << 8 | pesdata()[3]; }
    120121
    121     unsigned int TSSizeInBuffer() const
    122         { return (_cnt * TSPacket::PAYLOAD_SIZE) + TSPacket::HEADER_SIZE; }
     122    unsigned int TSSizeInBuffer() const { return _pesdataSize; }
    123123    unsigned int PSIOffset() const { return _psiOffset; }
    124124
    125125    const unsigned char* pesdata() const { return _pesdata; }
     
    188188  private:
    189189    uint _psiOffset;    ///< AFCOffset + StartOfFieldPointer
    190190    uint _ccLast;       ///< Continuity counter of last inserted TS Packet
    191     uint _cnt;          ///< Number of tspackets contained in PES Packet
    192191    uint _allocSize;    ///< number of bytes we allocated
     192    uint _pesdataSize;  ///< Number of bytes containing PES data
    193193    bool _badPacket;    ///< true if a CRC is not good yet
    194194};
    195195