File: Ap4DataBuffer.cpp

package info (click to toggle)
kodi-inputstream-adaptive 2.6.14%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,036 kB
  • sloc: cpp: 53,019; ansic: 492; makefile: 10
file content (225 lines) | stat: -rw-r--r-- 7,080 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*****************************************************************
|
|    AP4 - Data Buffer
|
|    Copyright 2002-2008 Axiomatic Systems, LLC
|
|
|    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
|    Unless you have obtained Bento4 under a difference license,
|    this version of Bento4 is Bento4|GPL.
|    Bento4|GPL 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, or (at your option)
|    any later version.
|
|    Bento4|GPL 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 Bento4|GPL; see the file COPYING.  If not, write to the
|    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|    02111-1307, USA.
|
 ****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "Ap4DataBuffer.h"
#include "Ap4Utils.h"

/*----------------------------------------------------------------------
|   AP4_DataBuffer::AP4_DataBuffer
+---------------------------------------------------------------------*/
AP4_DataBuffer::AP4_DataBuffer() :
    m_BufferIsLocal(true),
    m_Buffer(NULL),
    m_BufferSize(0),
    m_DataSize(0)
{
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::AP4_DataBuffer
+---------------------------------------------------------------------*/
AP4_DataBuffer::AP4_DataBuffer(AP4_Size buffer_size) :
    m_BufferIsLocal(true),
    m_Buffer(NULL),
    m_BufferSize(buffer_size),
    m_DataSize(0)
{
    m_Buffer = new AP4_Byte[buffer_size];
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::AP4_DataBuffer
+---------------------------------------------------------------------*/
AP4_DataBuffer::AP4_DataBuffer(const void* data, AP4_Size data_size) :
    m_BufferIsLocal(true),
    m_Buffer(NULL),
    m_BufferSize(data_size),
    m_DataSize(data_size)
{
    if (data && data_size) {
        m_Buffer = new AP4_Byte[data_size];
        AP4_CopyMemory(m_Buffer, data, data_size);
    }
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::AP4_DataBuffer
+---------------------------------------------------------------------*/
AP4_DataBuffer::AP4_DataBuffer(const AP4_DataBuffer& other) :
    m_BufferIsLocal(true),
    m_Buffer(NULL),
    m_BufferSize(other.m_DataSize),
    m_DataSize(other.m_DataSize)
{
    m_Buffer = new AP4_Byte[m_BufferSize];
    AP4_CopyMemory(m_Buffer, other.m_Buffer, m_BufferSize);
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::~AP4_DataBuffer
+---------------------------------------------------------------------*/
AP4_DataBuffer::~AP4_DataBuffer()
{
    if (m_BufferIsLocal) {
        delete[] m_Buffer;
    }
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::Reserve
+---------------------------------------------------------------------*/
AP4_Result
AP4_DataBuffer::Reserve(AP4_Size size)
{
    if (size <= m_BufferSize) return AP4_SUCCESS;

    // try doubling the buffer to accomodate for the new size
    AP4_Size new_size = m_BufferSize*2+1024;
    if (new_size < size) new_size = size;
    return SetBufferSize(new_size);
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::SetBuffer
+---------------------------------------------------------------------*/
AP4_Result
AP4_DataBuffer::SetBuffer(AP4_Byte* buffer, AP4_Size buffer_size)
{
    if (m_BufferIsLocal) {
        // destroy the local buffer
        delete[] m_Buffer;
    }

    // we're now using an external buffer
    m_BufferIsLocal = false;
    m_Buffer = buffer;
    m_BufferSize = buffer_size;

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::SetBufferSize
+---------------------------------------------------------------------*/
AP4_Result
AP4_DataBuffer::SetBufferSize(AP4_Size buffer_size)
{
    if (m_BufferIsLocal) {
        return ReallocateBuffer(buffer_size);
    } else {
        return AP4_FAILURE; // you cannot change the
                            // buffer management mode
    }
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::SetDataSize
+---------------------------------------------------------------------*/
AP4_Result
AP4_DataBuffer::SetDataSize(AP4_Size size)
{
    if (size > m_BufferSize) {
        if (m_BufferIsLocal) {
            AP4_Result result = ReallocateBuffer(size);
            if (AP4_FAILED(result)) return result;
        } else { 
            return AP4_FAILURE;
        }
    }
    m_DataSize = size;
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::SetData
+---------------------------------------------------------------------*/
AP4_Result
AP4_DataBuffer::SetData(const AP4_Byte* data, AP4_Size size)
{
    if (size > m_BufferSize) {
        if (m_BufferIsLocal) {
            AP4_Result result = ReallocateBuffer(size);
            if (AP4_FAILED(result)) return result;
        } else {
            return AP4_FAILURE;
        }
    }
    AP4_CopyMemory(m_Buffer, data, size);
    m_DataSize = size;

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::AppendData
+---------------------------------------------------------------------*/
AP4_Result
AP4_DataBuffer::AppendData(const AP4_Byte* data, AP4_Size data_size)
{
    if (data == NULL || data_size == 0) {
        return AP4_SUCCESS;
    }
    
    AP4_Size existing_size = m_DataSize;
    AP4_Result result = SetDataSize(existing_size+data_size);
    if (AP4_FAILED(result)) {
        return result;
    }
    AP4_CopyMemory(m_Buffer+existing_size, data, data_size);
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_DataBuffer::ReallocateBuffer
+---------------------------------------------------------------------*/
AP4_Result
AP4_DataBuffer::ReallocateBuffer(AP4_Size size)
{
    // check that the existing data fits
    if (m_DataSize > size) return AP4_FAILURE;

    // allocate a new buffer
    AP4_Byte* new_buffer = new AP4_Byte[size];

    // copy the contents of the previous buffer ,is any
	if (m_Buffer && m_DataSize) {
		AP4_CopyMemory(new_buffer, m_Buffer, m_DataSize);
	}

    // destroy the previous buffer
    delete[] m_Buffer;

    // use the new buffer
    m_Buffer = new_buffer;
    m_BufferSize = size;

    return AP4_SUCCESS;
}