Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Packet.cpp « Demuxers « demuxer - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a69d9ba9fcd6eab87e2ffd0c34943423ae26cd06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 *      Copyright (C) 2010-2012 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdafx.h>
#include "Packet.h"

Packet::Packet()
  : pmt(NULL)
  , bDiscontinuity(FALSE)
  , bSyncPoint(FALSE)
  , rtStart(INVALID_TIME)
  , rtStop(INVALID_TIME)
  , bPosition(-1)
  , dwFlags(0)
  , m_DataSize(0)
  , m_Data(NULL)
  , m_Buf(NULL)
{
}

Packet::~Packet()
{
  DeleteMediaType(pmt);
  av_buffer_unref(&m_Buf);
}

int Packet::SetDataSize(int len)
{
  if (len < 0)
    return -1;

  // RemoveHead may have moved m_Data, make sure the data moved too.
  if (m_Buf && m_Buf->data != m_Data)
    memmove(m_Buf->data, m_Data, m_DataSize);

  // Re-allocate the buffer, if required
  if (!m_Buf || m_Buf->size < (len + FF_INPUT_BUFFER_PADDING_SIZE)) {
    int ret = av_buffer_realloc(&m_Buf, len + FF_INPUT_BUFFER_PADDING_SIZE);
    if (ret < 0)
      return ret;
  }
  m_Data = m_Buf->data;
  m_DataSize = len;

  return 0;
}

int Packet::SetData(const void* ptr, int len)
{
  if (!ptr || len < 0)
    return -1;
  int ret = SetDataSize(len);
  if (ret < 0)
    return ret;

  memcpy(m_Data, ptr, len);
  return 0;
}

int Packet::SetPacket(AVPacket *pkt)
{
  if (pkt->buf) {
    ASSERT(!m_Buf);
    m_Buf = av_buffer_ref(pkt->buf);
    if (!m_Buf)
      return -1;
    m_Data = m_Buf->data;
    m_DataSize = pkt->size;
    return 0;
  } else {
    return SetData(pkt->data, pkt->size);
  }
}

int Packet::Append(Packet *ptr)
{
  return AppendData(ptr->GetData(), ptr->GetDataSize());
}

int Packet::AppendData(const void* ptr, int len)
{
  int prevSize = m_DataSize;
  int ret = SetDataSize(m_DataSize + len);
  if (ret < 0)
    return ret;
  memcpy(m_Data+prevSize, ptr, len);
  return 0;
}

int Packet::RemoveHead(int count)
{
  count = min(count, m_DataSize);
  if (!m_Data || count < 0)
    return -1;
  m_Data += count;
  m_DataSize -= count;
  return 0;
}